import pandas as pd
from sklearn.decomposition import PCA
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA

# 读取数据
data = pd.read_csv('wine.data', header=None)
# 设定列名
columns = ['Class', 'Alcohol', 'Malic_acid', 'Ash', 'Alcalinity_of_ash', 'Magnesium', 'Total_phenols',
           'Flavanoids', 'Nonflavanoid_phenols', 'Proanthocyanins', 'Color_intensity', 'Hue', 'OD280/OD315_of_diluted_wines', 'Proline']
data.columns = columns

# 提取类别标签为一类和二类的数据
selected_data = data[data['Class'].isin([1, 2])]

# 分离特征和标签
X = selected_data.drop('Class', axis=1)
y = selected_data['Class']

# 使用PCA进行降维
pca = PCA(n_components=2)
X_pca = pca.fit_transform(X)

# 输出PCA降维后的前5个样本的两维特征
print("PCA降维后的两维特征:")
print(X_pca[:5])

# 使用LDA进行降维
lda = LDA(n_components=2)
X_lda = lda.fit_transform(X, y)

# 输出LDA降维后的前5个样本的两维特征
print("\nLDA降维后的两维特征:")
print(X_lda[:5])
